package com.cosmoplat.gfqd.processor;

import com.cosmoplat.gfqd.config.BootStrapConfig;
import com.cosmoplat.gfqd.constants.Constants;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 创建archetype.properties到clone的项目中
 * 用于构建模板项目
 */
public class CreateArchetypePropertiesFile {

    private static String ARCHETYPE_GROUPID;
    private static String ARCHETYPE_ARTIFACTID;
    private static String ARCHETYPE_VERSION;
    private static String ARCHETYPE_PACKAGE;
    private static String NACOS_SERVER_ADDR;
    private static String NACOS_NAME_SPACE;
    private static String NEW_PROJECT_NAME;

    // 当所有项目克隆完成时调用的方法
    public static void create(BootStrapConfig config) {

        if (config != null) {
            System.out.println("Loaded bootstrap: " + config);
            // Access config values
            ARCHETYPE_GROUPID = config.getCreateProject().getArchetypeGroupId();
            ARCHETYPE_ARTIFACTID = config.getCreateProject().getArchetypeArtifactId();
            ARCHETYPE_VERSION = config.getCreateProject().getArchetypeVersion();
            ARCHETYPE_PACKAGE = config.getCreateProject().getArchetypePackage();
            NACOS_SERVER_ADDR = config.getGenerateProject().getNacosServerAddr();
            NACOS_NAME_SPACE = config.getGenerateProject().getNacosNameSpace();
            NEW_PROJECT_NAME = config.getGenerateProject().getNewProjectName();
        }

        // 指定输出路径
        String outputPath = Constants.GITLAB_PROJECTS +"/"+ Constants.ARCHETYPE_PROPERTIES_FILE;
        // 使用 StringBuilder 拼接内容
        StringBuilder content = new StringBuilder();

        //此处写死
        content.append("# 骨架模板项目所属组织名，该坐标必须声明（尽量不要修改）\n");
        content.append("archetype.groupId=").append(ARCHETYPE_GROUPID).append("\n");

        content.append("# 骨架模板项目名，最好约定以-archetype结尾\n");
        content.append("archetype.artifactId=").append(ARCHETYPE_ARTIFACTID).append("\n");

        content.append("# 骨架模板项目版本号\n");
        content.append("archetype.version=").append(ARCHETYPE_VERSION).append("\n");

        content.append("# 骨架模板项目包名,单个项目生成的时候有用，批量生成的时候没用\n");
        content.append("package=").append(ARCHETYPE_PACKAGE).append("\n");

        content.append("# 需要排除的文件或者目录 正则匹配，英文逗号隔开\n");
        content.append("excludePatterns=**/.idea/**,**/*.iml,/logs/**\n");

        content.append("# 需要进行属性变量替换的文件的后缀\n");
        content.append("archetype.filteredExtensions=java,xml,yml,yaml\n");

        content.append("# nacos地址\n");
        content.append("nacosServerAddr=").append(NACOS_SERVER_ADDR).append("\n");

        content.append("# nacos命名空间\n");
        content.append("nacosNameSpace=").append(NACOS_NAME_SPACE).append("\n");

        content.append("# 总项目名称\n");

        content.append("newProjectName=").append(string2Unicode(NEW_PROJECT_NAME)).append("\n");


        try (BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(outputPath), StandardCharsets.UTF_8))) {
            writer.write(content.toString());
            System.out.println("archetype.properties 文件已生成并保存到指定路径：" + outputPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String string2Unicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            // 取出每一个字符
            char c = string.charAt(i);
            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }
}